package com.yangyang.sp03.user.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yangyang.sp01.Service.UserService;
import com.yangyang.sp01.pojo.User;
import com.yangyang.web.util.JsonResult;
import com.yangyang.web.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
@RefreshScope
@Slf4j
@Service
public class UserServiceImpl implements UserService {
   @Value("${sp.user-service.users}")
   private String userJson;
    /*
        从demo数据中查询
        如果demo中没有数据，返回写死的数据
         */
    @Override
    public User getUser(Integer userId) {

       List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {
       });
       for (User u : list) {
           if (u.getId().equals(userId)) {
               return u;
           }
       }
       return new User(userId, "用户名"+userId,"密码"+userId);
    }


    @Override
    public void addScore(Integer id, Integer score) {
        log.info("user "+id+" - 增加积分 "+score);

    }
}
